package com.ming_za.od_updater.core.window.component;

import javax.swing.*;
import java.awt.*;

public class LoadingIcon extends JPanel {

    private float move = 0f;

    private final Timer timer;

    public LoadingIcon() {
        setPreferredSize(new Dimension(250, 20));
        setOpaque(false);

        // 创建动画定时器
        timer = new Timer(16, e -> {
            move += 0.02f;
            if (move > 1) {
                move = 0;
            }
            repaint();
        });
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        int x = (getWidth() - 250) / 2;
        int y = 0;

        g2d.clearRect(x,y,250,15);

        g2d.fillRect((int) (x + (move * 200)) + 1,y, 50,15);
        g2d.setColor(Color.GRAY);

        g2d.drawRect(x,y,250,15);
        g2d.setColor(Color.BLACK);

        g2d.dispose();
    }

    @Override
    public void removeNotify() {
        super.removeNotify();
        timer.stop();
    }
}
